﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using DreamLand.Services.Models;
using System.Xml.Linq;

namespace DreamLand.Services
{
    public class ServiceRepository
    {
        private XmlDocument xmlDoc;
        private string xmlPath = "Data/DreamLand.xml";

        public ServiceRepository()
        {
            xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);            
        }

        public List<Category> GetCategories()
        {
            List<Category> xeCategories = (from x in XElement.Load(xmlPath).Descendants("Category")
                                           where x.Attribute("parentid").Value == "null"
                                           select new Category
                                           {
                                               ID = int.Parse(x.Attribute("id").Value),
                                               Name = x.Element("Name").Value,
                                               Description = x.Element("Description").Value,
                                               Image = x.Element("Image").Value
                                           }).ToList();
            return xeCategories;
        }

        public List<Category> GetCategories(int parentID)
        {
            List<Category> xeCategories = (from x in XElement.Load(xmlPath).Descendants("Category")
                                           where x.Attribute("parentid").Value == parentID.ToString()
                                           select new Category
                                           {
                                               ID = int.Parse(x.Attribute("id").Value),
                                               Name = x.Element("Name").Value,
                                               Description = x.Element("Description").Value,
                                               Image = x.Element("Image").Value
                                           }).ToList();
            return xeCategories;
        }

        public List<Document> GetDocuments(int catID)
        {
            List<Document> xeDocument = (from x in XElement.Load(xmlPath).Descendants("Document")
                                         where x.Attribute("catid").Value == catID.ToString()
                                         select new Document
                                         {
                                             ID = int.Parse(x.Attribute("id").Value),
                                             Name = x.Element("Name").Value,
                                             Description = x.Element("Description").Value,
                                             UserName = x.Element("UserName").Value,
                                             Image = x.Element("Image").Value,
                                             PublishDate = x.Element("PublishDate").Value
                                         }).ToList();

            return xeDocument;
        }
    }
}